package com.tang.thepath.service;


import com.tang.thepath.model.BaseResponse;
import com.tang.thepath.model.RequestModel;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Body;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import rx.Observable;

public class UploadService extends BaseService{

    private interface API {

        @POST("/api/frontend/storefiles")
        Observable<BaseResponse<RequestModel.UploadResponse>> uploadImages(@Body RequestBody body);
    }


    public static Observable<RequestModel.UploadResponse> uploadImages(List<File> images){
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);

        for (int i=0;i<images.size();i++){
            File f = images.get(i);
            builder.addFormDataPart("images[]",f.getName(),RequestBody.create(MediaType.parse("images/*"),f));
        }

        RequestBody requestBody = builder.build();

        return toSubscribe(HttpManager.getService(API.class).uploadImages(requestBody));
    }
}
